package com.log.unit;

import com.log.exception.LogException;
import com.log.fun.FunChain;

import java.util.*;

/**
 * @ClassName LogContext
 * @Author xiezhuocai
 * @Description TODO
 * @Date 2022/5/11 20:52
 */
public class LogContext {

    public LogContext(String target, String stock, String mSpEL, String fun){
        if (stock == null||mSpEL==null||fun==null)
            throw new LogException(new StringBuilder().append("stock:").append(stock).append("mSpEL:").append(mSpEL).append("fun:").append(fun).append(" 不能为null").toString());
        this.bodyValue = target;
        this.stock = stock;
        this.mSpEL = mSpEL;
        this.fun = fun;
    }

    /**
     * @ClassName Resolver
     * @Author xiezhuocai
     * @Description 记录日志解析value
     * @Date 2022/5/11 20:52
     */
    public static class Resolver{

        private static final InheritableThreadLocal<Stack<Map<String, Object>>> variableMapStack = new InheritableThreadLocal<>();

        public synchronized static Map<String, Object> getVariables() {
            if (variableMapStack.get() == null) {
                putEmptySpan();
                return getVariables();
            }
            return variableMapStack.get().peek();
        }

        public synchronized static void putEmptySpan() {
            if (variableMapStack.get() == null) {
                Stack<Map<String, Object>> stack = new Stack<>();
                stack.push(new HashMap<>());
                variableMapStack.set(stack);
            }else {
                Stack<Map<String, Object>> stack = variableMapStack.get();
                stack.push(new HashMap<>());
                variableMapStack.set(stack);
            }
        }

        public static void putVariable(String k,Object v) {
            Map<String, Object> map = variableMapStack.get().peek();
            map.put(k,v);
        }

        public static void clear() {
            variableMapStack.get().pop();
        }
    }

    /**
     * 源参数
     */
    private String bodyValue;
    private String stock;
    private String mSpEL;
    private String fun;

    /**
     * 源参数Key
     */
    private List<String> bodyKey;
    private Map<String,String> stockKey;
    private Map<String,String> mSpELKey;
    private Map<String,String> funKey;

    /**
     * 是否要解析log
     */
    private boolean isSection;


    private static FunChain chain;


    public boolean isStock() {
        if(!this.isSection) return false;
        return !stockKey.isEmpty();
    }

    public boolean isMSpEL() {
        if(!this.isSection) return false;
        return !mSpELKey.isEmpty();
    }

    public boolean isFun() {
        if(!this.isSection) return false;
        return !funKey.isEmpty();
    }

    public String getBodyValue() {
        return bodyValue;
    }

    public boolean isSection(){
        this.isSection = !bodyKey.isEmpty();
        return this.isSection;
    }

    public List<String> getBodyKey() {
        return bodyKey;
    }

    public void setBodyKey(List<String> bodyKey) {
        this.bodyKey = bodyKey;
    }

    public Map<String,String> getStockKey() {
        return stockKey;
    }

    public void setStockKey(Map<String,String> stockKey) {
        this.stockKey = stockKey;
    }

    public Map<String,String> getmSpELKey() {
        return mSpELKey;
    }

    public void setmSpELKey(Map<String,String> mSpELKey) {
        this.mSpELKey = mSpELKey;
    }

    public Map<String,String> getFunKey() {
        return funKey;
    }

    public void setFunKey(Map<String,String> funKey) {
        this.funKey = funKey;
    }


    public static void setFunChain(FunChain setChain) {
        chain = setChain;
    }

    public FunChain getFunChain() {
        return chain;
    }
}
